package com.baor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baor.dao.MenuDao;
import com.baor.domain.MenuVo;
import com.baor.service.MenuService;
import com.baor.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Override
    public AjaxResult<?> menuList() {
        List<MenuVo> menuVos = menuDao.selectList(null);
        List<MenuVo> parents = menuVos.stream().filter( x -> x.getMenuSuperiors() == null ).collect(Collectors.toList());
        List<MenuVo> list = new ArrayList<>();
        for ( MenuVo vo : parents ){
            vo.setChilds(getChild(vo.getMenuId(),menuVos));
            list.add(vo);
        }
//        if(menuVo.getPageIndex() != null){
//            page = new Page<>(menuVo.getPageIndex(),menuVo.getPageSize());
//            map.put("list", cooperateDao.selectPage(page,null).getRecords());
//            map.put("pageCount", cooperateDao.selectPage(page, null).getPages());
//            return AjaxResult.success(map);
//        }
        return AjaxResult.success(list);
    }




    private List<MenuVo> getChild(Integer menuId, List<MenuVo> target){
        List<MenuVo> childs = target.stream().filter(x -> menuId.equals(x.getMenuSuperiors())).collect(Collectors.toList());
        if ( childs.size() != 0 )
            childs.forEach( x -> x.setChilds(getChild(x.getMenuId(),target)) );
        return childs;
    }

    @Override
    public AjaxResult insertMenu(MenuVo menuVo) {
        if ( menuVo.getMenuType().equals("0") )
            menuVo.setMenuType("菜单");
        else
            menuVo.setMenuType("按钮") ;
        return menuDao.insert(menuVo) > 0 ? AjaxResult.success(menuVo.getMenuId()) : AjaxResult.error("插入失败");
    }

    @Override
    public AjaxResult<?> update(MenuVo menuVo) {
        if ( menuVo.getMenuType() == null )
            menuVo.setMenuType("0");
        if ( menuVo.getMenuType().equals("0") )
            menuVo.setMenuType("菜单");
        else
            menuVo.setMenuType("按钮") ;
        return menuDao.updateById(menuVo) > 0 ? AjaxResult.success() : AjaxResult.error("更新失败");
    }

    @Override
    public AjaxResult<?> delete(Integer menuId) {
        List<MenuVo> menuVos = menuDao.selectList(null);
        deleteChilds(menuId,menuVos);
        return  AjaxResult.success() ;
    }

    private void deleteChilds(Integer menuId,List<MenuVo> menuVos){
        List<MenuVo> childs = menuDao.selectList(new LambdaQueryWrapper<MenuVo>().eq(MenuVo::getMenuSuperiors, menuId));
        if ( childs.size() != 0 )
            childs.forEach( x -> deleteChilds(x.getMenuId(),menuVos) );
        menuDao.deleteById(menuId);
    }

    @Override
    public AjaxResult<?> search(MenuVo menuVo) {
        if ( menuVo.getMenuName() == null )
            menuVo.setMenuName("");
        LambdaQueryWrapper<MenuVo> wrapper = new LambdaQueryWrapper<>();
        List<MenuVo> menuVos = menuDao.selectList(wrapper.like(MenuVo::getMenuName, menuVo.getMenuName()));
        return AjaxResult.success(menuVos);
    }

}
